# _*_ coding:utf-8 _*_
# @Time : 2023/5/25 17:57
# @Author : wsl
# @File : day05_demo
# @Project : python  案例https://blog.csdn.net/luxideyao/article/details/77802389
import json
import jsonpath
import urllib.request
import urllib.error

url = 'https://dianying.taobao.com/cityAction.json?city=index.htm&_ksTS=1685696577714_19&jsoncallback=jsonp20&action=cityAction&n_s=new&event_submit_doLocate=true'

headers = {
    # ':Authority': 'weibo.cn',
    # ':Method': 'GET',
    # ':Path': '/5351113868/info',
    # ':Scheme': 'https',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    # cookie携带登录信息
    'Cookie': 'cna=UMbAGcBZ7moCAT2qh7imKpzW; miid=378839864277494347; t=683df5b99e5d5695c5b38e0fecba0d3c; mt=ci%3D-1_1; thw=cn; cookie2=1e126e70a7cad13b003c798030cf4d70; v=0; _tb_token_=3b8b3d5ee36e; xlly_s=1; tb_city=110100; tb_cityName="sbG+qQ=="; isg=BOvrvNDZC0vZBVeEsJCM066Ueg_VAP-CB8jgn11qhCqB_Ape5dJg0sAdVjySXFd6; tfstk=ccyOBrZ2fwbMty7mLACH0ZNZQLshaw7xRhgDHRnukjfXydJvhs0LE8KFK1ijzVId.; l=fBSiK4EmNJ4wny1-BO5BFurza779qQAb8sPzaNbMiIEGa6tF9e2A2NC_5COpWdtjgTCfxetrip0J_dHvWM4dNxDDBeYO55AqcxvtaQtJe',
    'Pragma': 'no-cache',
    'Referer': 'https://weibo.cn/?tf=5_009',
    'Sec-Ch-Ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
    'Sec-Ch-Ua-Mobile': '?1',
    'Sec-Ch-Ua-Platform': '"Android"',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0'
}

request1 = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request1)
content1 = response.read().decode('utf-8')
content1 = content1.split('(')[1].split(')')[0]
print(content1)

with open('jsonpath.json', 'w', encoding='utf-8') as fp:
    fp.write(content1)

obj = json.load(open('jsonpath.json', 'r', encoding='utf-8'))
city_list = jsonpath.jsonpath(obj, '$..name')
print(city_list)
